﻿using Ewelve_Mouth_HD.Model;

namespace Ewelve_Mouth_HD.DAL
{
    public class TypeDal
    {
        private readonly Mycontext context;

        public TypeDal(Mycontext _context)
        {
            context = _context;
        }
        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="E"></param>
        /// <returns></returns>
        public int RoomTypeAdd(RoomType E)
        {
            try
            {
                var H = context.RoomTypes.Count(R => R.RoomName == E.RoomName);
                if (H > 0)
                {
                    return -1;
                }
                context.RoomTypes.Add(E);
                return context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 房型分页，查询显示
        /// </summary>
        /// <param name="RoomName"></param>
        /// <param name="MaxArea"></param>
        /// <param name="MinArea"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageSum"></param>
        /// <param name="PageCount"></param>
        /// <returns></returns>
        public IQueryable TypeShowAll(string? RoomName, int MaxArea, int MinArea, int PageIndex, int PageSize, out int PageSum, out int PageCount)
        {
            var K = context.RoomTypes.Where(L => L.IsDelete == false);
            if (!string.IsNullOrEmpty(RoomName))
            {
                K=K.Where(O=> O.RoomName == RoomName);
            }
            if(MaxArea!= 0)
            {
                K = K.Where(O => O.RoomArea <= MaxArea);
            }
            if (MinArea != 0)
            {
                K = K.Where(O => O.RoomArea >= MinArea);
            }
            PageSum = K.Count();
            PageCount =(int)Math.Ceiling((decimal)PageSum / PageSize);
            return K.OrderBy(O => O.RoomId).Skip((PageIndex-1)*PageSize).Take(PageSize);
        }
        /// <summary>
        /// 绑定下拉框
        /// </summary>
        /// <returns></returns>
        public IQueryable SelectShow()
        {
            return context.RoomTypes.Where(L => L.IsDelete == false).AsQueryable();
        }
    }
}
